home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 176-200 / scopedisk180 / patch / pch.c < prev    next >
C/C++ Source or Header  |  1995-03-19  |  36KB  |  1,130 lines

  1. /* $Header: pch.c,v 2.0.1.7 88/06/03 15:13:28 lwall Locked $
  2.  *
  3.  * $Log:        pch.c,v $
  4.  * Revision 2.0.1.7  88/06/03  15:13:28  lwall
  5.  * patch10: Can now find patches in shar scripts.
  6.  * patch10: Hunks that swapped and then swapped back could core dump.
  7.  *
  8.  * Revision 2.0.1.6  87/06/04  16:18:13  lwall
  9.  * pch_swap didn't swap p_bfake and p_efake.
  10.  *
  11.  * Revision 2.0.1.5  87/01/30  22:47:42  lwall
  12.  * Improved responses to mangled patches.
  13.  *
  14.  * Revision 2.0.1.4  87/01/05  16:59:53  lwall
  15.  * New-style context diffs caused double call to free().
  16.  *
  17.  * Revision 2.0.1.3  86/11/14  10:08:33  lwall
  18.  * Fixed problem where a long pattern wouldn't grow the hunk.
  19.  * Also restored p_input_line when backtracking so error messages are right.
  20.  *
  21.  * Revision 2.0.1.2  86/11/03  17:49:52  lwall
  22.  * New-style delete triggers spurious assertion error.
  23.  *
  24.  * Revision 2.0.1.1  86/10/29  15:52:08  lwall
  25.  * Could falsely report new-style context diff.
  26.  *
  27.  * Revision 2.0  86/09/17  15:39:37  lwall
  28.  * Baseline for netwide release.
  29.  *
  30.  */
  31.  
  32. #include "EXTERN.h"
  33. #include "common.h"
  34. #include "util.h"
  35. #include "INTERN.h"
  36. #include "pch.h"
  37.  
  38. /* Patch (diff listing) abstract type. */
  39.  
  40. static long p_filesize;                 /* size of the patch file */
  41. static LINENUM p_first;                 /* 1st line number */
  42. static LINENUM p_newfirst;              /* 1st line number of replacement */
  43. static LINENUM p_ptrn_lines;            /* # lines in pattern */
  44. static LINENUM p_repl_lines;            /* # lines in replacement text */
  45. static LINENUM p_end = -1;              /* last line in hunk */
  46. static LINENUM p_max;                   /* max allowed value of p_end */
  47. static LINENUM p_context = 3;           /* # of context lines */
  48. static LINENUM p_input_line = 0;        /* current line # from patch file */
  49. static char **p_line = Null(char**);    /* the text of the hunk */
  50. static short *p_len = Null(short*);     /* length of each line */
  51. static char *p_char = Nullch;           /* +, -, and ! */
  52. static int hunkmax = INITHUNKMAX;       /* size of above arrays to begin with */
  53. static int p_indent;                    /* indent to patch */
  54. static LINENUM p_base;                  /* where to intuit this time */
  55. static LINENUM p_bline;                 /* line # of p_base */
  56. static LINENUM p_start;                 /* where intuit found a patch */
  57. static LINENUM p_sline;                 /* and the line number for it */
  58. static LINENUM p_hunk_beg;              /* line number of current hunk */
  59. static LINENUM p_efake = -1;            /* end of faked up lines--don't free */
  60. static LINENUM p_bfake = -1;            /* beg of faked up lines */
  61.  
  62. /* Prepare to look for the next patch in the patch file. */
  63.  
  64. void
  65. re_patch()
  66. {
  67.     p_first = Nulline;
  68.     p_newfirst = Nulline;
  69.     p_ptrn_lines = Nulline;
  70.     p_repl_lines = Nulline;
  71.     p_end = (LINENUM)-1;
  72.     p_max = Nulline;
  73.     p_indent = 0;
  74. }
  75.  
  76. /* Open the patch file at the beginning of time. */
  77.  
  78. void
  79. open_patch_file(filename)
  80. char *filename;
  81. {
  82.     if (filename == Nullch || !*filename || strEQ(filename, "-")) {
  83.         pfp = fopen(TMPPATNAME, "w");
  84.         if (pfp == Nullfp)
  85.             fatal2("patch: can't create %s.\n", TMPPATNAME);
  86.         while (fgets(buf, sizeof buf, stdin) != Nullch)
  87.             fputs(buf, pfp);
  88.         Fclose(pfp);
  89.         filename = TMPPATNAME;
  90.     }
  91.     pfp = fopen(filename, "r");
  92.     if (pfp == Nullfp)
  93.         fatal2("patch file %s not found\n", filename);
  94. #ifndef AMIGA
  95.     Fstat(fileno(pfp), &filestat);
  96. #else
  97.     stat(filename,&filestat);
  98. #endif
  99.     p_filesize = filestat.st_size;
  100.  
  101.     next_intuit_at(0L);                 /* start at the beginning */
  102.  
  103.     set_hunkmax();
  104. }
  105.  
  106. /* Make sure our dynamically realloced tables are malloced to begin with. */
  107.  
  108. void
  109. set_hunkmax()
  110. {
  111. #ifndef lint
  112.     if (p_line == Null(char**))
  113.         p_line = (char**) malloc((MEM)hunkmax * sizeof(char *));
  114.     if (p_len == Null(short*))
  115.         p_len  = (short*) malloc((MEM)hunkmax * sizeof(short));
  116. #endif
  117.     if (p_char == Nullch)
  118.         p_char = (char*)  malloc((MEM)hunkmax * sizeof(char));
  119. }
  120.  
  121. /* Enlarge the arrays containing the current hunk of patch. */
  122.  
  123. void
  124. grow_hunkmax()
  125. {
  126.     hunkmax *= 2;
  127.     /*
  128.      * Note that on most systems, only the p_line array ever gets fresh memory
  129.      * since p_len can move into p_line's old space, and p_char can move into
  130.      * p_len's old space.  Not on PDP-11's however.  But it doesn't matter.
  131.      */
  132.     assert(p_line != Null(char**) && p_len != Null(short*) && p_char != Nullch);
  133. #ifndef lint
  134.     p_line = (char**) realloc((char*)p_line, (MEM)hunkmax * sizeof(char *));
  135.     p_len  = (short*) realloc((char*)p_len,  (MEM)hunkmax * sizeof(short));
  136.     p_char = (char*)  realloc((char*)p_char, (MEM)hunkmax * sizeof(char));
  137. #endif
  138.     if (p_line != Null(char**) && p_len != Null(short*) && p_char != Nullch)
  139.         return;
  140.     if (!using_plan_a)
  141.         fatal1("patch: out of memory (grow_hunkmax)\n");
  142.     out_of_mem = TRUE;          /* whatever is null will be allocated again */
  143.                                 /* from within plan_a(), of all places */
  144. }
  145.  
  146. /* True if the remainder of the patch file contains a diff of some sort. */
  147.  
  148. bool
  149. there_is_another_patch()
  150. {
  151.     if (p_base != 0L && p_base >= p_filesize) {
  152.         if (verbose)
  153.             say1("done\n");
  154.         return FALSE;
  155.     }
  156.     if (verbose)
  157.         say1("Hmm...");
  158.     diff_type = intuit_diff_type();
  159.     if (!diff_type) {
  160.         if (p_base != 0L) {
  161.             if (verbose)
  162.                 say1("  Ignoring the trailing garbage.\ndone\n");
  163.         }
  164.         else
  165.             say1("  I can't seem to find a patch in there anywhere.\n");
  166.         return FALSE;
  167.     }
  168.     if (verbose)
  169.         say3("  %sooks like %s to me...\n",
  170.             (p_base == 0L ? "L" : "The next patch l"),
  171.             diff_type == CONTEXT_DIFF ? "a context diff" :
  172.             diff_type == NEW_CONTEXT_DIFF ? "a new-style context diff" :
  173.             diff_type == NORMAL_DIFF ? "a normal diff" :
  174.             "an ed script" );
  175.     if (p_indent && verbose)
  176.         say3("(Patch is indented %d space%s.)\n", p_indent, p_indent==1?"":"s");
  177.     skip_to(p_start,p_sline);
  178.     while (filearg[0] == Nullch) {
  179.         if (force) {
  180.             say1("No file to patch.  Skipping...\n");
  181.             filearg[0] = savestr(bestguess);
  182.             return TRUE;
  183.         }
  184.         ask1("File to patch: ");
  185.         if (*buf != '\n') {
  186.             if (bestguess)
  187.                 free(bestguess);
  188.             bestguess = savestr(buf);
  189.             filearg[0] = fetchname(buf, 0, FALSE);
  190.         }
  191.         if (filearg[0] == Nullch) {
  192.             ask1("No file found--skip this patch? [n] ");
  193.             if (*buf != 'y') {
  194.                 continue;
  195.             }
  196.             if (verbose)
  197.                 say1("Skipping patch...\n");
  198.             filearg[0] = fetchname(bestguess, 0, TRUE);
  199.             skip_rest_of_patch = TRUE;
  200.             return TRUE;
  201.         }
  202.     }
  203.     return TRUE;
  204. }
  205.  
  206. /* Determine what kind of diff is in the remaining part of the patch file. */
  207.  
  208. int
  209. intuit_diff_type()
  210. {
  211.     Reg4 long this_line = 0;
  212.     Reg5 long previous_line;
  213.     Reg6 long first_command_line = -1;
  214.     long fcl_line;
  215.     Reg7 bool last_line_was_command = FALSE;
  216.     Reg8 bool this_is_a_command = FALSE;
  217.     Reg9 bool stars_last_line = FALSE;
  218.     Reg10 bool stars_this_line = FALSE;
  219.     Reg3 int indent;
  220.     Reg1 char *s;
  221.     Reg2 char *t;
  222.     char *indtmp = Nullch;
  223.     char *oldtmp = Nullch;
  224.     char *newtmp = Nullch;
  225.     char *indname = Nullch;
  226.     char *oldname = Nullch;
  227.     char *newname = Nullch;
  228.     Reg11 int retval;
  229.     bool no_filearg = (filearg[0] == Nullch);
  230.  
  231.     ok_to_create_file = FALSE;
  232.     Fseek(pfp, p_base, 0);
  233.     p_input_line = p_bline - 1;
  234.     for (;;) {
  235.         previous_line = this_line;
  236.         last_line_was_command = this_is_a_command;
  237.         stars_last_line = stars_this_line;
  238.         this_line = ftell(pfp);
  239.         indent = 0;
  240.         p_input_line++;
  241.         if (fgets(buf, sizeof buf, pfp) == Nullch) {
  242.             if (first_command_line >= 0L) {
  243.                                         /* nothing but deletes!? */
  244.                 p_start = first_command_line;
  245.                 p_sline = fcl_line;
  246.                 retval = ED_DIFF;
  247.                 goto scan_exit;
  248.             }
  249.             else {
  250.                 p_start = this_line;
  251.                 p_sline = p_input_line;
  252.                 retval = 0;
  253.                 goto scan_exit;
  254.             }
  255.         }
  256.         for (s = buf; *s == ' ' || *s == '\t' || *s == 'X'; s++) {
  257.             if (*s == '\t')
  258.                 indent += 8 - (indent % 8);
  259.             else
  260.                 indent++;
  261.         }
  262.         for (t=s; isdigit(*t) || *t == ','; t++) ;
  263.         this_is_a_command = (isdigit(*s) &&
  264.           (*t == 'd' || *t == 'c' || *t == 'a') );
  265.         if (first_command_line < 0L && this_is_a_command) {
  266.             first_command_line = this_line;
  267.             fcl_line = p_input_line;
  268.             p_indent = indent;          /* assume this for now */
  269.         }
  270.         if (!stars_last_line && strnEQ(s, "*** ", 4))
  271.             oldtmp = savestr(s+4);
  272.         else if (strnEQ(s, "--- ", 4))
  273.             newtmp = savestr(s+4);
  274.         else if (strnEQ(s, "Index:", 6))
  275.             indtmp = savestr(s+6);
  276.         else if (strnEQ(s, "Prereq:", 7)) {
  277.             for (t=s+7; isspace(*t); t++) ;
  278.             revision = savestr(t);
  279.             for (t=revision; *t && !isspace(*t); t++) ;
  280.             *t = '\0';
  281.             if (!*revision) {
  282.                 free(revision);
  283.                 revision = Nullch;
  284.             }
  285.         }
  286.         if ((!diff_type || diff_type == ED_DIFF) &&
  287.           first_command_line >= 0L &&
  288.           strEQ(s, ".\n") ) {
  289.             p_indent = indent;
  290.             p_start = first_command_line;
  291.             p_sline = fcl_line;
  292.             retval = ED_DIFF;
  293.             goto scan_exit;
  294.         }
  295.         stars_this_line = strnEQ(s, "********", 8);
  296.         if ((!diff_type || diff_type == CONTEXT_DIFF) && stars_last_line &&
  297.                  strnEQ(s, "*** ", 4)) {
  298.             if (!atol(s+4))
  299.                 ok_to_create_file = TRUE;
  300.             /* if this is a new context diff the character just before */
  301.             /* the newline is a '*'. */
  302.             while (*s != '\n')
  303.                 s++;
  304.             p_indent = indent;
  305.             p_start = previous_line;
  306.             p_sline = p_input_line - 1;
  307.             retval = (*(s-1) == '*' ? NEW_CONTEXT_DIFF : CONTEXT_DIFF);
  308.             goto scan_exit;
  309.         }
  310.         if ((!diff_type || diff_type == NORMAL_DIFF) &&
  311.           last_line_was_command &&
  312.           (strnEQ(s, "< ", 2) || strnEQ(s, "> ", 2)) ) {
  313.             p_start = previous_line;
  314.             p_sline = p_input_line - 1;
  315.             p_indent = indent;
  316.             retval = NORMAL_DIFF;
  317.             goto scan_exit;
  318.         }
  319.     }
  320.   scan_exit:
  321.     if (no_filearg) {
  322.         if (indtmp != Nullch)
  323.             indname = fetchname(indtmp, strippath, ok_to_create_file);
  324.         if (oldtmp != Nullch)
  325.             oldname = fetchname(oldtmp, strippath, ok_to_create_file);
  326.         if (newtmp != Nullch)
  327.             newname = fetchname(newtmp, strippath, ok_to_create_file);
  328.         if (oldname && newname) {
  329.             if (strlen(oldname) < strlen(newname))
  330.                 filearg[0] = savestr(oldname);
  331.             else
  332.                 filearg[0] = savestr(newname);
  333.         }
  334.         else if (oldname)
  335.             filearg[0] = savestr(oldname);
  336.         else if (newname)
  337.             filearg[0] = savestr(newname);
  338.         else if (indname)
  339.             filearg[0] = savestr(indname);
  340.     }
  341.     if (bestguess) {
  342.         free(bestguess);
  343.         bestguess = Nullch;
  344.     }
  345.     if (filearg[0] != Nullch)
  346.         bestguess = savestr(filearg[0]);
  347.     else if (indtmp != Nullch)
  348.         bestguess = fetchname(indtmp, strippath, TRUE);
  349.     else {
  350.         if (oldtmp != Nullch)
  351.             oldname = fetchname(oldtmp, strippath, TRUE);
  352.         if (newtmp != Nullch)
  353.             newname = fetchname(newtmp, strippath, TRUE);
  354.         if (oldname && newname) {
  355.             if (strlen(oldname) < strlen(newname))
  356.                 bestguess = savestr(oldname);
  357.             else
  358.                 bestguess = savestr(newname);
  359.         }
  360.         else if (oldname)
  361.             bestguess = savestr(oldname);
  362.         else if (newname)
  363.             bestguess = savestr(newname);
  364.     }
  365.     if (indtmp != Nullch)
  366.         free(indtmp);
  367.     if (oldtmp != Nullch)
  368.         free(oldtmp);
  369.     if (newtmp != Nullch)
  370.         free(newtmp);
  371.     if (indname != Nullch)
  372.         free(indname);
  373.     if (oldname != Nullch)
  374.         free(oldname);
  375.     if (newname != Nullch)
  376.         free(newname);
  377.     return retval;
  378. }
  379.  
  380. /* Remember where this patch ends so we know where to start up again. */
  381.  
  382. void
  383. next_intuit_at(file_pos,file_line)
  384. long file_pos;
  385. long file_line;
  386. {
  387.     p_base = file_pos;
  388.     p_bline = file_line;
  389. }
  390.  
  391. /* Basically a verbose fseek() to the actual diff listing. */
  392.  
  393. void
  394. skip_to(file_pos,file_line)
  395. long file_pos;
  396. long file_line;
  397. {
  398.     char *ret;
  399.  
  400.     assert(p_base <= file_pos);
  401.     if (verbose && p_base < file_pos) {
  402.         Fseek(pfp, p_base, 0);
  403.         say1("The text leading up to this was:\n--------------------------\n");
  404.         while (ftell(pfp) < file_pos) {
  405.             ret = fgets(buf, sizeof buf, pfp);
  406.             assert(ret != Nullch);
  407.             say2("|%s", buf);
  408.         }
  409.         say1("--------------------------\n");
  410.     }
  411.     else
  412.         Fseek(pfp, file_pos, 0);
  413.     p_input_line = file_line - 1;
  414. }
  415.  
  416. /* True if there is more of the current diff listing to process. */
  417.  
  418. bool
  419. another_hunk()
  420. {
  421.     Reg1 char *s;
  422.     Reg8 char *ret;
  423.     Reg2 int context = 0;
  424.  
  425.     while (p_end >= 0) {
  426.         if (p_end == p_efake)
  427.             p_end = p_bfake;            /* don't free twice */
  428.         else
  429.             free(p_line[p_end]);
  430.         p_end--;
  431.     }
  432.     assert(p_end == -1);
  433.     p_efake = -1;
  434.  
  435.     p_max = hunkmax;                    /* gets reduced when --- found */
  436.     if (diff_type == CONTEXT_DIFF || diff_type == NEW_CONTEXT_DIFF) {
  437.         long line_beginning = ftell(pfp);
  438.                                         /* file pos of the current line */
  439.         LINENUM repl_beginning = 0;     /* index of --- line */
  440.         Reg4 LINENUM fillcnt = 0;       /* #lines of missing ptrn or repl */
  441.         Reg5 LINENUM fillsrc;           /* index of first line to copy */
  442.         Reg6 LINENUM filldst;           /* index of first missing line */
  443.         bool ptrn_spaces_eaten = FALSE; /* ptrn was slightly misformed */
  444.         Reg9 bool repl_could_be_missing = TRUE;
  445.                                         /* no + or ! lines in this hunk */
  446.         bool repl_missing = FALSE;      /* we are now backtracking */
  447.         long repl_backtrack_position = 0;
  448.                                         /* file pos of first repl line */
  449.         LINENUM repl_patch_line;        /* input line number for same */
  450.         Reg7 LINENUM ptrn_copiable = 0;
  451.                                         /* # of copiable lines in ptrn */
  452.  
  453.         ret = pgets(buf, sizeof buf, pfp);
  454.         p_input_line++;
  455.         if (ret == Nullch || strnNE(buf, "********", 8)) {
  456.             next_intuit_at(line_beginning,p_input_line);
  457.             return FALSE;
  458.         }
  459.         p_context = 100;
  460.         p_hunk_beg = p_input_line + 1;
  461.         while (p_end < p_max) {
  462.             line_beginning = ftell(pfp);
  463.             ret = pgets(buf, sizeof buf, pfp);
  464.             p_input_line++;
  465.             if (ret == Nullch) {
  466.                 if (p_max - p_end < 4)
  467.                     Strcpy(buf, "  \n");  /* assume blank lines got chopped */
  468.                 else {
  469.                     if (repl_beginning && repl_could_be_missing) {
  470.                         repl_missing = TRUE;
  471.                         goto hunk_done;
  472.                     }
  473.                     fatal1("Unexpected end of file in patch.\n");
  474.                 }
  475.             }
  476.             p_end++;
  477.             assert(p_end < hunkmax);
  478.             p_char[p_end] = *buf;
  479. #ifdef zilog
  480.             p_line[(short)p_end] = Nullch;
  481. #else
  482.             p_line[p_end] = Nullch;
  483. #endif
  484.             switch (*buf) {
  485.             case '*':
  486.                 if (strnEQ(buf, "********", 8)) {
  487.                     if (repl_beginning && repl_could_be_missing) {
  488.                         repl_missing = TRUE;
  489.                         goto hunk_done;
  490.                     }
  491.                     else
  492.                         fatal2("Unexpected end of hunk at line %ld.\n",
  493.                             p_input_line);
  494.                 }
  495.                 if (p_end != 0) {
  496.                     if (repl_beginning && repl_could_be_missing) {
  497.                         repl_missing = TRUE;
  498.                         goto hunk_done;
  499.                     }
  500.                     fatal3("Unexpected *** at line %ld: %s", p_input_line, buf);
  501.                 }
  502.                 context = 0;
  503.                 p_line[p_end] = savestr(buf);
  504.                 if (out_of_mem) {
  505.                     p_end--;
  506.                     return FALSE;
  507.                 }
  508.                 for (s=buf; *s && !isdigit(*s); s++) ;
  509.                 if (!*s)
  510.                     goto malformed;
  511.                 if (strnEQ(s,"0,0",3))
  512.                     strcpy(s,s+2);
  513.                 p_first = (LINENUM) atol(s);
  514.                 while (isdigit(*s)) s++;
  515.                 if (*s == ',') {
  516.                     for (; *s && !isdigit(*s); s++) ;
  517.                     if (!*s)
  518.                         goto malformed;
  519.                     p_ptrn_lines = ((LINENUM)atol(s)) - p_first + 1;
  520.                 }
  521.                 else if (p_first)
  522.                     p_ptrn_lines = 1;
  523.                 else {
  524.                     p_ptrn_lines = 0;
  525.                     p_first = 1;
  526.                 }
  527.                 p_max = p_ptrn_lines + 6;       /* we need this much at least */
  528.                 while (p_max >= hunkmax)
  529.                     grow_hunkmax();
  530.                 p_max = hunkmax;
  531.                 break;
  532.             case '-':
  533.                 if (buf[1] == '-') {
  534.                     if (repl_beginning ||
  535.                         (p_end != p_ptrn_lines + 1 + (p_char[p_end-1] == '\n')))
  536.                     {
  537.                         if (p_end == 1) {
  538.                             /* `old' lines were omitted - set up to fill */
  539.                             /* them in from 'new' context lines. */
  540.                             p_end = p_ptrn_lines + 1;
  541.                             fillsrc = p_end + 1;
  542.                             filldst = 1;
  543.                             fillcnt = p_ptrn_lines;
  544.                         }
  545.                         else {
  546.                             if (repl_beginning) {
  547.                                 if (repl_could_be_missing){
  548.                                     repl_missing = TRUE;
  549.                                     goto hunk_done;
  550.                                 }
  551.                                 fatal3(
  552. "Duplicate \"---\" at line %ld--check line numbers at line %ld.\n",
  553.                                     p_input_line, p_hunk_beg + repl_beginning);
  554.                             }
  555.                             else {
  556.                                 fatal4(
  557. "%s \"---\" at line %ld--check line numbers at line %ld.\n",
  558.                                     (p_end <= p_ptrn_lines
  559.                                         ? "Premature"
  560.                                         : "Overdue" ),
  561.                                     p_input_line, p_hunk_beg);
  562.                             }
  563.                         }
  564.                     }
  565.                     repl_beginning = p_end;
  566.                     repl_backtrack_position = ftell(pfp);
  567.                     repl_patch_line = p_input_line;
  568.                     p_line[p_end] = savestr(buf);
  569.                     if (out_of_mem) {
  570.                         p_end--;
  571.                         return FALSE;
  572.                     }
  573.                     p_char[p_end] = '=';
  574.                     for (s=buf; *s && !isdigit(*s); s++) ;
  575.                     if (!*s)
  576.                         goto malformed;
  577.                     p_newfirst = (LINENUM) atol(s);
  578.                     while (isdigit(*s)) s++;
  579.                     if (*s == ',') {
  580.                         for (; *s && !isdigit(*s); s++) ;
  581.                         if (!*s)
  582.                             goto malformed;
  583.                         p_repl_lines = ((LINENUM)atol(s)) - p_newfirst + 1;
  584.                     }
  585.                     else if (p_newfirst)
  586.                         p_repl_lines = 1;
  587.                     else {
  588.                         p_repl_lines = 0;
  589.                         p_newfirst = 1;
  590.                     }
  591.                     p_max = p_repl_lines + p_end;
  592.                     if (p_max > MAXHUNKSIZE)
  593.                         fatal4("Hunk too large (%ld lines) at line %ld: %s",
  594.                               p_max, p_input_line, buf);
  595.                     while (p_max >= hunkmax)
  596.                         grow_hunkmax();
  597.                     if (p_repl_lines != ptrn_copiable)
  598.                         repl_could_be_missing = FALSE;
  599.                     break;
  600.                 }
  601.                 goto change_line;
  602.             case '+':  case '!':
  603.                 repl_could_be_missing = FALSE;
  604.               change_line:
  605.                 if (buf[1] == '\n' && canonicalize)
  606.                     strcpy(buf+1," \n");
  607.                 if (!isspace(buf[1]) && buf[1] != '>' && buf[1] != '<' &&
  608.                   repl_beginning && repl_could_be_missing) {
  609.                     repl_missing = TRUE;
  610.                     goto hunk_done;
  611.                 }
  612.                 if (context > 0) {
  613.                     if (context < p_context)
  614.                         p_context = context;
  615.                     context = -1000;
  616.                 }
  617.                 p_line[p_end] = savestr(buf+2);
  618.                 if (out_of_mem) {
  619.                     p_end--;
  620.                     return FALSE;
  621.                 }
  622.                 break;
  623.             case '\t': case '\n':       /* assume the 2 spaces got eaten */
  624.                 if (repl_beginning && repl_could_be_missing &&
  625.                   (!ptrn_spaces_eaten || diff_type == NEW_CONTEXT_DIFF) ) {
  626.                     repl_missing = TRUE;
  627.                     goto hunk_done;
  628.                 }
  629.                 p_line[p_end] = savestr(buf);
  630.                 if (out_of_mem) {
  631.                     p_end--;
  632.                     return FALSE;
  633.                 }
  634.                 if (p_end != p_ptrn_lines + 1) {
  635.                     ptrn_spaces_eaten |= (repl_beginning != 0);
  636.                     context++;
  637.                     if (!repl_beginning)
  638.                         ptrn_copiable++;
  639.                     p_char[p_end] = ' ';
  640.                 }
  641.                 break;
  642.             case ' ':
  643.                 if (!isspace(buf[1]) &&
  644.                   repl_beginning && repl_could_be_missing) {
  645.                     repl_missing = TRUE;
  646.                     goto hunk_done;
  647.                 }
  648.                 context++;
  649.                 if (!repl_beginning)
  650.                     ptrn_copiable++;
  651.                 p_line[p_end] = savestr(buf+2);
  652.                 if (out_of_mem) {
  653.                     p_end--;
  654.                     return FALSE;
  655.                 }
  656.                 break;
  657.             default:
  658.                 if (repl_beginning && repl_could_be_missing) {
  659.                     repl_missing = TRUE;
  660.                     goto hunk_done;
  661.                 }
  662.                 goto malformed;
  663.             }
  664.             /* set up p_len for strncmp() so we don't have to */
  665.             /* assume null termination */
  666.             if (p_line[p_end])
  667.                 p_len[p_end] = strlen(p_line[p_end]);
  668.             else
  669.                 p_len[p_end] = 0;
  670.         }
  671.  
  672.     hunk_done:
  673.         if (p_end >=0 && !repl_beginning)
  674.             fatal2("No --- found in patch at line %ld\n", pch_hunk_beg());
  675.  
  676.         if (repl_missing) {
  677.  
  678.             /* reset state back to just after --- */
  679.             p_input_line = repl_patch_line;
  680.             for (p_end--; p_end > repl_beginning; p_end--)
  681.                 free(p_line[p_end]);
  682.             Fseek(pfp, repl_backtrack_position, 0);
  683.  
  684.             /* redundant 'new' context lines were omitted - set */
  685.             /* up to fill them in from the old file context */
  686.             fillsrc = 1;
  687.             filldst = repl_beginning+1;
  688.             fillcnt = p_repl_lines;
  689.             p_end = p_max;
  690.         }
  691.  
  692.         if (diff_type == CONTEXT_DIFF &&
  693.           (fillcnt || (p_first > 1 && ptrn_copiable > 2*p_context)) ) {
  694.             if (verbose)
  695.                 say4("%s\n%s\n%s\n",
  696. "(Fascinating--this is really a new-style context diff but without",
  697. "the telltale extra asterisks on the *** line that usually indicate",
  698. "the new style...)");
  699.             diff_type = NEW_CONTEXT_DIFF;
  700.         }
  701.  
  702.         /* if there were omitted context lines, fill them in now */
  703.         if (fillcnt) {
  704.             p_bfake = filldst;          /* remember where not to free() */
  705.             p_efake = filldst + fillcnt - 1;
  706.             while (fillcnt-- > 0) {
  707.                 while (fillsrc <= p_end && p_char[fillsrc] != ' ')
  708.                     fillsrc++;
  709.                 if (fillsrc > p_end)
  710.                     fatal2("Replacement text or line numbers mangled in hunk at line %ld\n",
  711.                         p_hunk_beg);
  712.                 p_line[filldst] = p_line[fillsrc];
  713.                 p_char[filldst] = p_char[fillsrc];
  714.                 p_len[filldst] = p_len[fillsrc];
  715.                 fillsrc++; filldst++;
  716.             }
  717.             while (fillsrc <= p_end && fillsrc != repl_beginning &&
  718.               p_char[fillsrc] != ' ')
  719.                 fillsrc++;
  720. #ifdef DEBUGGING
  721.             if (debug & 64)
  722.                 printf("fillsrc %ld, filldst %ld, rb %ld, e+1 %ld\n",
  723.                     fillsrc,filldst,repl_beginning,p_end+1);
  724. #endif
  725.             assert(fillsrc==p_end+1 || fillsrc==repl_beginning);
  726.             assert(filldst==p_end+1 || filldst==repl_beginning);
  727.         }
  728.     }
  729.     else {                              /* normal diff--fake it up */
  730.         char hunk_type;
  731.         Reg3 int i;
  732.         LINENUM min, max;
  733.         long line_beginning = ftell(pfp);
  734.  
  735.         p_context = 0;
  736.         ret = pgets(buf, sizeof buf, pfp);
  737.         p_input_line++;
  738.         if (ret == Nullch || !isdigit(*buf)) {
  739.             next_intuit_at(line_beginning,p_input_line);
  740.             return FALSE;
  741.         }
  742.         p_first = (LINENUM)atol(buf);
  743.         for (s=buf; isdigit(*s); s++) ;
  744.         if (*s == ',') {
  745.             p_ptrn_lines = (LINENUM)atol(++s) - p_first + 1;
  746.             while (isdigit(*s)) s++;
  747.         }
  748.         else
  749.             p_ptrn_lines = (*s != 'a');
  750.         hunk_type = *s;
  751.         if (hunk_type == 'a')
  752.             p_first++;                  /* do append rather than insert */
  753.         min = (LINENUM)atol(++s);
  754.         for (; isdigit(*s); s++) ;
  755.         if (*s == ',')
  756.             max = (LINENUM)atol(++s);
  757.         else
  758.             max = min;
  759.         if (hunk_type == 'd')
  760.             min++;
  761.         p_end = p_ptrn_lines + 1 + max - min + 1;
  762.         if (p_end > MAXHUNKSIZE)
  763.             fatal4("Hunk too large (%ld lines) at line %ld: %s",
  764.                   p_end, p_input_line, buf);
  765.         while (p_end >= hunkmax)
  766.             grow_hunkmax();
  767.         p_newfirst = min;
  768.         p_repl_lines = max - min + 1;
  769.         Sprintf(buf, "*** %ld,%ld\n", p_first, p_first + p_ptrn_lines - 1);
  770.         p_line[0] = savestr(buf);
  771.         if (out_of_mem) {
  772.             p_end = -1;
  773.             return FALSE;
  774.         }
  775.         p_char[0] = '*';
  776.         for (i=1; i<=p_ptrn_lines; i++) {
  777.             ret = pgets(buf, sizeof buf, pfp);
  778.             p_input_line++;
  779.             if (ret == Nullch)
  780.                 fatal2("Unexpected end of file in patch at line %ld.\n",
  781.                   p_input_line);
  782.             if (*buf != '<')
  783.                 fatal2("< expected at line %ld of patch.\n", p_input_line);
  784.             p_line[i] = savestr(buf+2);
  785.             if (out_of_mem) {
  786.                 p_end = i-1;
  787.                 return FALSE;
  788.             }
  789.             p_len[i] = strlen(p_line[i]);
  790.             p_char[i] = '-';
  791.         }
  792.         if (hunk_type == 'c') {
  793.             ret = pgets(buf, sizeof buf, pfp);
  794.             p_input_line++;
  795.             if (ret == Nullch)
  796.                 fatal2("Unexpected end of file in patch at line %ld.\n",
  797.                     p_input_line);
  798.             if (*buf != '-')
  799.                 fatal2("--- expected at line %ld of patch.\n", p_input_line);
  800.         }
  801.         Sprintf(buf, "--- %ld,%ld\n", min, max);
  802.         p_line[i] = savestr(buf);
  803.         if (out_of_mem) {
  804.             p_end = i-1;
  805.             return FALSE;
  806.         }
  807.         p_char[i] = '=';
  808.         for (i++; i<=p_end; i++) {
  809.             ret = pgets(buf, sizeof buf, pfp);
  810.             p_input_line++;
  811.             if (ret == Nullch)
  812.                 fatal2("Unexpected end of file in patch at line %ld.\n",
  813.                     p_input_line);
  814.             if (*buf != '>')
  815.                 fatal2("> expected at line %ld of patch.\n", p_input_line);
  816.             p_line[i] = savestr(buf+2);
  817.             if (out_of_mem) {
  818.                 p_end = i-1;
  819.                 return FALSE;
  820.             }
  821.             p_len[i] = strlen(p_line[i]);
  822.             p_char[i] = '+';
  823.         }
  824.     }
  825.     if (reverse)                        /* backwards patch? */
  826.         if (!pch_swap())
  827.             say1("Not enough memory to swap next hunk!\n");
  828. #ifdef DEBUGGING
  829.     if (debug & 2) {
  830.         int i;
  831.         char special;
  832.  
  833.         for (i=0; i <= p_end; i++) {
  834.             if (i == p_ptrn_lines)
  835.                 special = '^';
  836.             else
  837.                 special = ' ';
  838.             fprintf(stderr, "%3d %c %c %s", i, p_char[i], special, p_line[i]);
  839.             Fflush(stderr);
  840.         }
  841.     }
  842. #endif
  843.     if (p_end+1 < hunkmax)      /* paranoia reigns supreme... */
  844.         p_char[p_end+1] = '^';  /* add a stopper for apply_hunk */
  845.     return TRUE;
  846.  
  847. malformed:
  848.     fatal3("Malformed patch at line %ld: %s", p_input_line, buf);
  849.                 /* about as informative as "Syntax error" in C */
  850.     return FALSE;       /* for lint */
  851. }
  852.  
  853. /* Input a line from the patch file, worrying about indentation. */
  854.  
  855. char *
  856. pgets(bf,sz,fp)
  857. char *bf;
  858. int sz;
  859. FILE *fp;
  860. {
  861.     char *ret = fgets(bf, sz, fp);
  862.     Reg1 char *s;
  863.     Reg2 int indent = 0;
  864.  
  865.     if (p_indent && ret != Nullch) {
  866.         for (s=buf;
  867.           indent < p_indent && (*s == ' ' || *s == '\t' || *s == 'X'); s++) {
  868.             if (*s == '\t')
  869.                 indent += 8 - (indent % 7);
  870.             else
  871.                 indent++;
  872.         }
  873.         if (buf != s)
  874.             Strcpy(buf, s);
  875.     }
  876.     return ret;
  877. }
  878.  
  879. /* Reverse the old and new portions of the current hunk. */
  880.  
  881. bool
  882. pch_swap()
  883. {
  884.     char **tp_line;             /* the text of the hunk */
  885.     short *tp_len;              /* length of each line */
  886.     char *tp_char;              /* +, -, and ! */
  887.     Reg1 LINENUM i;
  888.     Reg2 LINENUM n;
  889.     bool blankline = FALSE;
  890.     Reg3 char *s;
  891.  
  892.     i = p_first;
  893.     p_first = p_newfirst;
  894.     p_newfirst = i;
  895.  
  896.     /* make a scratch copy */
  897.  
  898.     tp_line = p_line;
  899.     tp_len = p_len;
  900.     tp_char = p_char;
  901.     p_line = Null(char**);      /* force set_hunkmax to allocate again */
  902.     p_len = Null(short*);
  903.     p_char = Nullch;
  904.     set_hunkmax();
  905.     if (p_line == Null(char**) || p_len == Null(short*) || p_char == Nullch) {
  906. #ifndef lint
  907.         if (p_line == Null(char**))
  908.             free((char*)p_line);
  909.         p_line = tp_line;
  910.         if (p_len == Null(short*))
  911.             free((char*)p_len);
  912.         p_len = tp_len;
  913. #endif
  914.         if (p_char == Nullch)
  915.             free((char*)p_char);
  916.         p_char = tp_char;
  917.         return FALSE;           /* not enough memory to swap hunk! */
  918.     }
  919.  
  920.     /* now turn the new into the old */
  921.  
  922.     i = p_ptrn_lines + 1;
  923.     if (tp_char[i] == '\n') {           /* account for possible blank line */
  924.         blankline = TRUE;
  925.         i++;
  926.     }
  927.     if (p_efake >= 0) {                 /* fix non-freeable ptr range */
  928.         if (p_efake <= i)
  929.             n = p_end - i + 1;
  930.         else
  931.             n = -i;
  932.         p_efake += n;
  933.         p_bfake += n;
  934.     }
  935.     for (n=0; i <= p_end; i++,n++) {
  936.         p_line[n] = tp_line[i];
  937.         p_char[n] = tp_char[i];
  938.         if (p_char[n] == '+')
  939.             p_char[n] = '-';
  940.         p_len[n] = tp_len[i];
  941.     }
  942.     if (blankline) {
  943.         i = p_ptrn_lines + 1;
  944.         p_line[n] = tp_line[i];
  945.         p_char[n] = tp_char[i];
  946.         p_len[n] = tp_len[i];
  947.         n++;
  948.     }
  949.     assert(p_char[0] == '=');
  950.     p_char[0] = '*';
  951.     for (s=p_line[0]; *s; s++)
  952.         if (*s == '-')
  953.             *s = '*';
  954.  
  955.     /* now turn the old into the new */
  956.  
  957.     assert(tp_char[0] == '*');
  958.     tp_char[0] = '=';
  959.     for (s=tp_line[0]; *s; s++)
  960.         if (*s == '*')
  961.             *s = '-';
  962.     for (i=0; n <= p_end; i++,n++) {
  963.         p_line[n] = tp_line[i];
  964.         p_char[n] = tp_char[i];
  965.         if (p_char[n] == '-')
  966.             p_char[n] = '+';
  967.         p_len[n] = tp_len[i];
  968.     }
  969.     assert(i == p_ptrn_lines + 1);
  970.     i = p_ptrn_lines;
  971.     p_ptrn_lines = p_repl_lines;
  972.     p_repl_lines = i;
  973. #ifndef lint
  974.     if (tp_line == Null(char**))
  975.         free((char*)tp_line);
  976.     if (tp_len == Null(short*))
  977.         free((char*)tp_len);
  978. #endif
  979.     if (tp_char == Nullch)
  980.         free((char*)tp_char);
  981.     return TRUE;
  982. }
  983.  
  984. /* Return the specified line position in the old file of the old context. */
  985.  
  986. LINENUM
  987. pch_first()
  988. {
  989.     return p_first;
  990. }
  991.  
  992. /* Return the number of lines of old context. */
  993.  
  994. LINENUM
  995. pch_ptrn_lines()
  996. {
  997.     return p_ptrn_lines;
  998. }
  999.  
  1000. /* Return the probable line position in the new file of the first line. */
  1001.  
  1002. LINENUM
  1003. pch_newfirst()
  1004. {
  1005.     return p_newfirst;
  1006. }
  1007.  
  1008. /* Return the number of lines in the replacement text including context. */
  1009.  
  1010. LINENUM
  1011. pch_repl_lines()
  1012. {
  1013.     return p_repl_lines;
  1014. }
  1015.  
  1016. /* Return the number of lines in the whole hunk. */
  1017.  
  1018. LINENUM
  1019. pch_end()
  1020. {
  1021.     return p_end;
  1022. }
  1023.  
  1024. /* Return the number of context lines before the first changed line. */
  1025.  
  1026. LINENUM
  1027. pch_context()
  1028. {
  1029.     return p_context;
  1030. }
  1031.  
  1032. /* Return the length of a particular patch line. */
  1033.  
  1034. short
  1035. pch_line_len(line)
  1036. LINENUM line;
  1037. {
  1038.     return p_len[line];
  1039. }
  1040.  
  1041. /* Return the control character (+, -, *, !, etc) for a patch line. */
  1042.  
  1043. char
  1044. pch_char(line)
  1045. LINENUM line;
  1046. {
  1047.     return p_char[line];
  1048. }
  1049.  
  1050. /* Return a pointer to a particular patch line. */
  1051.  
  1052. char *
  1053. pfetch(line)
  1054. LINENUM line;
  1055. {
  1056.     return p_line[line];
  1057. }
  1058.  
  1059. /* Return where in the patch file this hunk began, for error messages. */
  1060.  
  1061. LINENUM
  1062. pch_hunk_beg()
  1063. {
  1064.     return p_hunk_beg;
  1065. }
  1066.  
  1067. /* Apply an ed script by feeding ed itself. */
  1068.  
  1069. void
  1070. do_ed_script()
  1071. {
  1072.     Reg1 char *t;
  1073.     Reg2 long beginning_of_this_line;
  1074.     Reg3 bool this_line_is_command = FALSE;
  1075.     Reg4 FILE *pipefp;
  1076.     FILE *popen();
  1077.  
  1078.     if (!skip_rest_of_patch) {
  1079.         Unlink(TMPOUTNAME);
  1080.         copy_file(filearg[0], TMPOUTNAME);
  1081.         if (verbose)
  1082.             Sprintf(buf, "/bin/ed %s", TMPOUTNAME);
  1083.         else
  1084.             Sprintf(buf, "/bin/ed - %s", TMPOUTNAME);
  1085.         pipefp = popen(buf, "w");
  1086.     }
  1087.     for (;;) {
  1088.         beginning_of_this_line = ftell(pfp);
  1089.         if (pgets(buf, sizeof buf, pfp) == Nullch) {
  1090.             next_intuit_at(beginning_of_this_line,p_input_line);
  1091.             break;
  1092.         }
  1093.         p_input_line++;
  1094.         for (t=buf; isdigit(*t) || *t == ','; t++) ;
  1095.         this_line_is_command = (isdigit(*buf) &&
  1096.           (*t == 'd' || *t == 'c' || *t == 'a') );
  1097.         if (this_line_is_command) {
  1098.             if (!skip_rest_of_patch)
  1099.                 fputs(buf, pipefp);
  1100.             if (*t != 'd') {
  1101.                 while (pgets(buf, sizeof buf, pfp) != Nullch) {
  1102.                     p_input_line++;
  1103.                     if (!skip_rest_of_patch)
  1104.                         fputs(buf, pipefp);
  1105.                     if (strEQ(buf, ".\n"))
  1106.                         break;
  1107.                 }
  1108.             }
  1109.         }
  1110.         else {
  1111.             next_intuit_at(beginning_of_this_line,p_input_line);
  1112.             break;
  1113.         }
  1114.     }
  1115.     if (skip_rest_of_patch)
  1116.         return;
  1117.     fprintf(pipefp, "w\n");
  1118.     fprintf(pipefp, "q\n");
  1119.     Fflush(pipefp);
  1120.     Pclose(pipefp);
  1121.     ignore_signals();
  1122.     if (move_file(TMPOUTNAME, outname) < 0) {
  1123.         toutkeep = TRUE;
  1124.         chmod(TMPOUTNAME, filemode);
  1125.     }
  1126.     else
  1127.         chmod(outname, filemode);
  1128.     set_signals(1);
  1129. }
  1130.